package com.fowo.api.service.handleWork;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.fowo.api.inventory.entity.Inventory;
import com.fowo.api.inventory.mapper.InventoryMapper;
import com.fowo.api.inventory.model.InventoryStorageVo;
import com.fowo.api.inventory.model.SimpleInventoryItem;
import com.fowo.api.model.inventory.list.subtable.InventoryListSubtableItemVo;
import com.fowo.api.model.requisition.RequisitionReleaseinventoryAction;
import com.fowo.api.model.requisition.RequisitionVo;
import com.fowo.api.model.requisition.product.RequisitionProductItemVo;
import com.fowo.api.service.RequisitionService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 调拨单的释放库存行为接口
 */
@Component
public class RequisitionReleaseinventory implements RequisitionReleaseinventoryAction {

    @Resource
    private RequisitionService requisitionService;
    @Resource
    private InventoryMapper inventoryMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void run(List<Long> idList) throws Exception {
        for(Long id : idList){
            // 释放库存
            requisitionService.releaseGoodInventory(id);
            requisitionService.releaseGoodInventoryBin(id);

            RequisitionVo requisition = requisitionService.getVoById(id);
            final Integer badSum = requisition.getRequisitionProduct().stream().filter(x->x.getDefectiveRqtQuantity()!=null).mapToInt(p -> p.getDefectiveRqtQuantity()).sum();
            if(badSum > 0){
                requisitionService.releaseBadInventory(id);
                requisitionService.releaseBadInventoryBin(id);
            }
            //单据改为待配货
            requisition.setStatus("0");
            boolean update = requisitionService.update(requisition);
        }
    }

    /**
     * 释放库存
     * @param requisition
     */
    public void releaseInventory(RequisitionVo requisition) throws Exception {
        for(RequisitionProductItemVo subtable : requisition.getRequisitionProduct()){
            SimpleInventoryItem inventoryItem = new SimpleInventoryItem();
            inventoryItem.setId(subtable.getProductId());

            InventoryStorageVo inventoryStorageVo = new InventoryStorageVo();
            inventoryStorageVo.setStockId(requisition.getWid());
            inventoryStorageVo.setItem(inventoryItem);
            inventoryStorageVo.setShopId(subtable.getShopName() == null ? null : subtable.getShopName().toString());

            List<Inventory> inventoryList =inventoryMapper.selectByStorageOne(inventoryStorageVo);
            Inventory inventory = null;
            if (CollectionUtils.isNotEmpty(inventoryList)){
                inventory=inventoryList.get(0);
            }else {
//                throw new Exception("当前仓库没有库存");
                throw new Exception("当前仓库没有产品 "+subtable.getProductIdName()+" 的库存");
            }

            LambdaUpdateChainWrapper<Inventory> inventoryUpdateChainWrapper = new LambdaUpdateChainWrapper<>(inventoryMapper);
            inventoryUpdateChainWrapper.eq(Inventory::getId, inventory.getId());
            inventoryUpdateChainWrapper.eq(Inventory::getVersion, inventory.getVersion());
            inventoryUpdateChainWrapper.set(Inventory::getVersion, inventory.getVersion() + 1);
            inventoryUpdateChainWrapper.set(Inventory::getLockQty, new BigDecimal(0));
            inventoryUpdateChainWrapper.set(Inventory::getStockQty, inventory.getLockQty());
            if(!inventoryUpdateChainWrapper.update()) {
                throw new Exception("数据版本错误，更新失败");
            }
        }
    }
}
